package com.yanqu.road.dao.impl.activity.specialtombrob;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.specialtombrob.SpecialTombRobUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class SpecialTombRobUserDataDaoImpl extends TempDataObjectDao<SpecialTombRobUserData> {

    public static String getSpecialTombRobUserDataSql = "select * from `t_u_activity_special_tomb_rob_user_data` where `userId`=?";
    public SpecialTombRobUserData getSpecialTombRobUserData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSpecialTombRobUserDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateSpecialTombRobUserDataSql = "update `t_u_activity_special_tomb_rob_user_data` set guideBit=? where userId=?";
    @Override
    protected boolean updateData(SpecialTombRobUserData specialTombRobUserData) {
        specialTombRobUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, specialTombRobUserData.getGuideBit()));
        para.put(i++, new DbParameter(Types.BIGINT, specialTombRobUserData.getUserId()));
        if (getDb().executeNoneQuery(updateSpecialTombRobUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSpecialTombRobUserDataSql = "insert into `t_u_activity_special_tomb_rob_user_data`(userId,guideBit) values (?,?)";
    @Override
    protected boolean addData(SpecialTombRobUserData specialTombRobUserData) {
        specialTombRobUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, specialTombRobUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, specialTombRobUserData.getGuideBit()));
        if (getDb().executeNoneQuery(insertSpecialTombRobUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SpecialTombRobUserData parseData(ResultSet rs) throws SQLException {
        SpecialTombRobUserData specialTombRobUserData = new SpecialTombRobUserData();
        specialTombRobUserData.setUserId(rs.getLong("userId"));
        specialTombRobUserData.setGuideBit(rs.getInt("guideBit"));

        return specialTombRobUserData;
    }
}
